package com.sprouting.personal.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.personal.annotations.ApiIdempotent;
import com.sprouting.personal.interfaces.common.Constant;
import com.sprouting.personal.model.common.ResultJson;
import com.sprouting.personal.model.common.jsr.AddGroup;
import com.sprouting.personal.model.common.jsr.UpdGroup;
import com.sprouting.personal.model.entity.PsJotDown;
import com.sprouting.personal.service.PsJotDownService;
import com.sprouting.personal.utils.CheckTool;
import com.sprouting.personal.utils.EntityTool;
import com.sprouting.personal.utils.IdTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 随手记
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2022/12/20 16:29
 */
@RequestMapping("/jotDown")
@RestController
public class PsJotDownController {

    @Autowired
    private PsJotDownService psJotDownService;

    /**
     * 分页查询随手记
     * @param page 分页参数
     * @param checkValue 是否选中，非必填
     * @return
     */
    @PostMapping("/getJotDownListPage")
    public ResultJson getJotDownListPage(Page page, Integer checkValue){
        String userId = StpUtil.getLoginIdAsString();
        return ResultJson.success(psJotDownService.getJotDownListPage(page, checkValue, userId));
    }

    /**
     * 保存随手记
     * @param psJotDown 随手记数据
     * @return
     */
    @PostMapping("/saveJotDown")
    @ApiIdempotent
    public ResultJson saveJotDown(@Validated(AddGroup.class) PsJotDown psJotDown){
        if (psJotDown.getMarkColor() == null){
            psJotDown.setMarkColor(Constant.MarkColor.NOTHING);
        }
        if (psJotDown.getCheckValue() == null){
            psJotDown.setCheckValue(Constant.YesOrNo.NO);
        }

        psJotDown.setId(IdTool.getId());
        psJotDown.setUserId(StpUtil.getLoginIdAsString());
        EntityTool.buildSaveBase(psJotDown, StpUtil.getLoginIdAsString());
        psJotDownService.save(psJotDown);
        return ResultJson.success(psJotDown);
    }

    /**
     * 更新随手记
     * @param psJotDown 随手记数据
     * @return
     */
    @PostMapping("/updJotDown")
    @ApiIdempotent
    public ResultJson updJotDown(@Validated(UpdGroup.class) PsJotDown psJotDown){
        EntityTool.buildUpdBase(psJotDown, StpUtil.getLoginIdAsString());
        psJotDownService.updateById(psJotDown);
        return ResultJson.success(psJotDown);
    }

    /**
     * 根据主键删除随手记
     * @param id 随手记主键
     * @return
     */
    @ApiIdempotent
    @PostMapping("/delJotDown")
    public ResultJson delJotDown(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorIllegal();
        }

        psJotDownService.removeById(id);
        return ResultJson.success();
    }

    /**
     * 切换随手记状态
     * @param id 主键
     * @return
     */
    @ApiIdempotent
    @PostMapping("/switchCheck")
    public ResultJson switchCheck(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorIllegal();
        }

        PsJotDown jotDown = psJotDownService.getJotDownById(id);
        if (jotDown == null){
            return ResultJson.errorPram();
        }
        Integer checkValue = jotDown.getCheckValue();
        if (Constant.YesOrNo.YES.equals(checkValue)){
            jotDown.setCheckValue(Constant.YesOrNo.NO);
        } else {
            jotDown.setCheckValue(Constant.YesOrNo.YES);
        }
        EntityTool.buildUpdBase(jotDown, StpUtil.getLoginIdAsString());
        psJotDownService.updateById(jotDown);
        return ResultJson.success();
    }

}
